package  base.controller;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import base.model.Student;
import base.service.StudentService;
import base.pageModel.base.Json;
import common.exception.ServiceException;
import common.utils.ExceptionUtil;



/**
 * 
 * @author xiaozewen
 * @version  2016-11-25 19:53:54
 */
@Controller
@RequestMapping(value="/student")
public class StudentController {
	 @Autowired
	private StudentService studentService;
	
	@RequestMapping("/goAdd")
	public String goAdd(){
		return "/system/student/add";
	}
	
	@RequestMapping("/goUpdate")
	public String goUpdate(Integer studentId,HttpServletRequest request){
		request.setAttribute("student",studentService.selectByPrimaryKey(studentId));
		return "/system/student/update";
	}
	
	@RequestMapping("/goShow")
	public String goShow(Integer studentId,HttpServletRequest request){
		request.setAttribute("student",studentService.selectByPrimaryKey(studentId));
		return "/system/student/show";
	}
	
	@RequestMapping("/goList")
	public String goList(){
		return "/system/student/list";
	}
	
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Json add(Student student){
		Json json=new Json();
		try {
			studentService.save(student);
		}catch (ServiceException s) {
			ExceptionUtil.fillMsg(s, json);
		}catch (Exception e) {
			ExceptionUtil.errorMsg(e, json);
		}
		return json;
	}
	
	@RequestMapping(value="/del",method=RequestMethod.POST)
	@ResponseBody
	public Json del(String ids){
		Json json=new Json();
		try {
			studentService.delete(ids);
		}catch (ServiceException s) {
			ExceptionUtil.fillMsg(s, json);
		}catch (Exception e) {
			ExceptionUtil.errorMsg(e, json);
		}
		return json;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Json update(Student student){
		Json json=new Json();
		try {
			studentService.updateAll(student);
		} catch (ServiceException s) {
			ExceptionUtil.fillMsg(s, json);
		}catch (Exception e) {
			ExceptionUtil.errorMsg(e, json);
		}
		return json;
	}
	
	@RequestMapping("/getById")
	@ResponseBody
	public Json getById(Integer studentId){
		Json json=new Json();
		try {
			json.setData(studentService.selectByPrimaryKey(studentId));
		} catch (ServiceException s) {
			ExceptionUtil.fillMsg(s, json);
		}catch (Exception e) {
			ExceptionUtil.errorMsg(e, json);
		}
		return json;
	}
	
	
	@RequestMapping("/listPage.json")
	@ResponseBody
	public Json listPage(Student student,Integer pageNum,Integer pageSize){
		Json json=new Json();
		try {
			PageInfo<Student> pageInfo = studentService.listPage(student,pageNum,pageSize);
			json.setData(pageInfo);
		} catch (ServiceException s) {
			ExceptionUtil.fillMsg(s, json);
		}catch (Exception e) {
			ExceptionUtil.errorMsg(e, json);
		}
		return json;
	}
	
	@RequestMapping("/listAll.json")
	@ResponseBody
	public Json listAll(Student student){
		Json json=new Json();
		try {
			List<Student> students = studentService.list(student);
			json.setData(students);
		} catch (ServiceException s) {
			ExceptionUtil.fillMsg(s, json);
		}catch (Exception e) {
			ExceptionUtil.errorMsg(e, json);
		}
		return json;
	}
	
	@RequestMapping("/search")
	@ResponseBody
	public Json search(String name){
		Json json=new Json();
		try {
			List<Map<String, Object>> students = studentService.search(name);
			json.setData(students);
		} catch (ServiceException s) {
			ExceptionUtil.fillMsg(s, json);
		}catch (Exception e) {
			ExceptionUtil.errorMsg(e, json);
		}
		return json;
	}
}
